package com.ziyueyuwen.xiniao.module;

import org.apache.commons.lang3.StringUtils;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author jun 2018/12/4
 */
public class UserLoginCache {

    private static class LazyHolder {
        private static final UserLoginCache OUR_INSTANCE = new UserLoginCache();
    }

    public static UserLoginCache getInstance() {
        return LazyHolder.OUR_INSTANCE;
    }

    private UserLoginCache() {
    }

    private final ConcurrentHashMap<String, String> userLoginMap = new ConcurrentHashMap<>();

    public void add(String userId, String sessionId) {
        if (StringUtils.isNotBlank(userId) && StringUtils.isNotBlank(sessionId)) {
            userLoginMap.put(userId, sessionId);
        }
    }

    public Boolean isLogin(String userId, String sessionId) {
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(sessionId)) {
            return false;
        }
        return sessionId.equals(userLoginMap.get(userId));
    }
}
